from django.shortcuts import render
from django.core.paginator import Paginator
from .models import Product,News

# Create your views here.
def home(request):
    return render(request, 'home.html')


def science(request):
    return render(request, 'science.html')


def about(request, type):
    # 不同的请求地址，返回同一个页面，但是数据不同，展现的信息不同
    return render(request, 'about.html', {'type': type})


def product(request, type):
    """产品页"""
    types = ['家用机器人', '智能监控', '人脸识别解决方案']
    #  根据type查询数据库中的数据信息传递到前端进行解析
    result = Product.objects.filter(productType=type)
    # 分页操作
    paginator = Paginator(result,2)
    # 获取前端页面的页码，如果没有传递则默认给1
    num = request.GET.get("num",1)
    # 根据页码获取数据
    page = paginator.page(num)

    return render(request, 'product.html', {'type': type,'title':types[type-1],'data':page})

def product_detail(request,product_id):
    """产品详情页"""
    # 根据前端传递的产品ID获取对应的产品信息
    product = Product.objects.filter(id=product_id)
    code = 1
    if product :
        product = product[0]
    else:
        product = '没有查询到相关数据~'
        code = 0
    return render(request, 'product_detail.html', {'code':code ,'data': product})
    
def new(request, type):
    types = ['企业新闻', '行业新闻', '通知公告']

    # 模型名称.objects.all|filter|order_by...
    result = News.objects.filter(newType=type)

    # 分页
    paginator = Paginator(result, 4)
    # 获取前端页面的页码，如果没有传递则默认给1
    num = request.GET.get("num", 1)
    # 根据页码获取数据
    page = paginator.page(num)

    return render(request, 'new.html', {'type': type,'title':types[type-1],'data':page})

def new_detail(request,new_id):
    new = News.objects.filter(id=new_id)
    code = 1
    if new:
        new = new[0]
    else:
        new = '没有查询到相关新闻~'
        code = 0
    return render(request, 'new_detail.html', {'code': code, 'data': new})



def service(request, type):
    return render(request, 'service.html', {'type': type})




def contact(request, type):
    return render(request, 'contact.html', {'type': type})